<template>
  <div>
    <!-- 表单 -->
    <el-form :model="formDatas" ref="formDatas" class="el-form el-forms hcenter">
      <el-form-item prop="cardNum">
        <el-input placeholder="会员卡号" v-model.trim="formDatas.cardNum"></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input placeholder="会员姓名" v-model.trim="formDatas.name"></el-input>
      </el-form-item>
      <el-form-item prop="payType">
       <el-select v-model="formDatas.payType" placeholder="支付类型" class="el-select">
          <el-option :label="item.name" v-for="(item, index) in payType" :value="item.type" :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="birthday">
        <el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="出生日期" v-model="formDatas.birthday" style="width: 100%;"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearchMember">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="hanldeAddMember">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleSearchReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" height="250" border style="width: 100%">
      <el-table-column type="index" prop="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="cardNum" label="会员卡号"></el-table-column>
      <el-table-column prop="name" label="会员姓名" width="100"> </el-table-column>
      <el-table-column prop="birthday" label="会员生日">
        <template slot-scope="scope">
          {{$Time.timeFormat(scope.row.birthday)}}
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号码"> </el-table-column>
      <el-table-column prop="integral" label="可用积分"> </el-table-column>
      <el-table-column prop="money" label="开卡金额"> </el-table-column>
      <el-table-column prop="payType" label="支付类型" width="100">
        <template slot-scope="scope">
          {{scope.row.payType | filterPayType}}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="会员地址"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleOpenEditDialog(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDeleteMember(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="obj.pageNum"
      :page-sizes="[10, 20, 50]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 新增弹出框 -->
    <el-dialog title="会员新增" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="addform" :rules="rules" ref="addform">
        <el-form-item label="会员卡号" prop="cardNum" label-width="100px">
          <el-input placeholder="会员卡号" v-model.trim="addform.cardNum"></el-input>
        </el-form-item>
        <el-form-item prop="name" label="会员姓名" label-width="100px">
          <el-input placeholder="会员姓名" v-model.trim="addform.name"></el-input>
        </el-form-item>
        <el-form-item prop="birthday" label="会员生日" label-width="100px">
          <el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="会员生日" v-model="addform.birthday" style="width: 200px"></el-date-picker>
        </el-form-item>
        <el-form-item prop="phone" label="手机号码" label-width="100px">
          <el-input v-model.trim="addform.phone"></el-input>
        </el-form-item>
        <el-form-item prop="money" label="开卡金额" label-width="100px">
          <el-input v-model.trim="addform.money"></el-input>
        </el-form-item>
        <el-form-item prop="integral" label="可用积分" label-width="100px">
          <el-input v-model.trim="addform.integral"></el-input>
        </el-form-item>
        <el-form-item prop="payType" label="支付类型" label-width="100px">
          <el-select v-model="addform.payType" placeholder="支付类型" class="el-select">
            <el-option :label="item.name" v-for="(item, index) in payType" :value="item.type" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员地址" prop="address" label-width="100px">
          <el-input type="textarea" v-model="addform.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetform('addform')">取 消</el-button>
        <el-button type="primary" @click="handleAddSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑弹出框 -->
    <el-dialog title="会员编辑" :visible.sync="dialogEditFormVisible" width="500px">
      <el-form :model="editform" :rules="rules" ref="editform">
        <el-form-item label="会员卡号" prop="cardNum" label-width="100px">
          <el-input placeholder="会员卡号" v-model.trim="editform.cardNum"></el-input>
        </el-form-item>
        <el-form-item prop="name" label="会员姓名" label-width="100px">
          <el-input placeholder="会员姓名" v-model.trim="editform.name"></el-input>
        </el-form-item>
        <el-form-item prop="birthday" label="会员生日" label-width="100px">
          <el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="会员生日" v-model="editform.birthday" style="width: 200px"></el-date-picker>
        </el-form-item>
        <el-form-item prop="phone" label="手机号码" label-width="100px">
          <el-input v-model.trim="editform.phone"></el-input>
        </el-form-item>
        <el-form-item prop="money" label="开卡金额" label-width="100px">
          <el-input v-model.trim="editform.money"></el-input>
        </el-form-item>
        <el-form-item prop="integral" label="可用积分" label-width="100px">
          <el-input v-model.trim="editform.integral"></el-input>
        </el-form-item>
        <el-form-item prop="payType" label="支付类型" label-width="100px">
          <el-select v-model="editform.payType" placeholder="支付类型" class="el-select">
            <el-option v-for="(item, index) in payType" :label="item.name" :value="item.type" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员地址" prop="address" label-width="100px">
          <el-input type="textarea" v-model="editform.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetform('editform')">取 消</el-button>
        <el-button type="primary" @click="handleEditSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 支付类型
const payType = [
        {
          type: 0,
          name: '现金'
        },
        {
          type: 1,
          name: '微信'
        },
        {
          type: 2,
          name: '支付宝'
        },
        {
          type: 3,
          name: '银行卡'
        }
      ]
// import myMain from '../components/myMain.vue'
import membermodel from '../api/member.js'
export default {
  data() {
    return {
      rules: {
          cardNum: [
            { required: true, message: '请输入会员卡号', trigger: 'blur' },
            {min :5, max : 20, message: "长度在 5 到 20 个字符",trigger: "blur"}
          ],
          name: [
            { required: true, message: '请输入会员姓名', trigger: 'blur' },
            {min :2, max : 10, message: "长度在 2 到 10 个字符",trigger: "blur"}
          ],
          payType: [
            { required: true, message: '请输入支付类型', trigger: 'change' }
          ]
      },
      // 查询表单内容
      formDatas: {
        cardNum: '',
        name: '',
        payType: '',
        birthday: ''
      },
      // 页码和条数
      obj: {
        pageNum: 1,
        pageSize: 10,
      },
      // 总条数
      total: 0,
      tableData: [],
      // 新增弹出框
      dialogFormVisible:false,
      // 编辑弹出框
      dialogEditFormVisible: false,
      // 新增表单内容
      addform:{
        cardNum: '',
        name: '',
        payType: '',
        birthday: '',
        phone: '',
        // 开卡金额
        money: 0,
        // 积分
        integral: 0,
        // 会员地址
        address: ''
      },
      // 编辑表单内容
      editform:{
        cardNum: '',
        name: '',
        payType: '',
        birthday: '',
        phone: '',
        // 开卡金额
        money: 0,
        // 积分
        integral: 0,
        // 会员地址
        address: ''
      },
      payType
    }
  },
  created() {
    // 获取列表数据
    this.initMemberList()
  },
  // 过滤支付类型
  filters: {
    filterPayType (value) {
      const obj = payType.find(item => item.type == value)
      return obj?obj.name:''
    }
  },
  methods: {
    // 获取列表数据
    async initMemberList(){
      const response = await membermodel.getMemberList(this.obj,this.formDatas)
      // 获取表格数据
      this.tableData = response.msg.rows
      // 获取总条数
      this.total = response.msg.count
    },
    // 点击页码
    handleCurrentChange(page){
      this.obj.pageNum = page
      this.initMemberList()
    },
    // 点击条数
    handleSizeChange(size){
      this.obj.pageSize = size
      this.initMemberList()
    },
    // 点击查询
    handleSearchMember(){
      this.initMemberList()
    },
    // 点击重置
    handleSearchReset(){
      this.$refs["formDatas"].resetFields();
    },
    // 点击删除
    handleDeleteMember(row){
      const id = row.id
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const response = await membermodel.removeMember(id)
        if(response.error_code == 0) {
          this.initMemberList()
          this.$message({
          type: 'success',
          message: '删除成功!'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    // 点击新增
    hanldeAddMember() {
      this.dialogFormVisible = true
    },
    // 点击确定实现新增
    handleAddSubmit() {
      this.$refs['addform'].validate(async valid => {
        if(!valid) return
        const response = await membermodel.addMember(this.addform)
        if(response.error_code == 0) {
          this.$message('新增成功')
          this.dialogFormVisible = false
          this.$refs["addform"].resetFields()
          this.initMemberList()
        }
      })
    },
    // 点击确定实现编辑
    handleEditSubmit(row){
      const memberId = this.editform.id
      this.$refs['editform'].validate(async valid => {
        if(!valid) return
        const response = await membermodel.editMember(memberId,this.editform)
        if(response.error_code == 0) {
          this.$message('更新成功')
          this.dialogEditFormVisible = false
          this.$refs["editform"].resetFields()
          this.initMemberList()
        }
      })
    },
    // 表单重置方法
    resetform(formName) {
      this.$refs[formName].resetFields()
      if(formName === "addform") {
        this.dialogFormVisible = false
      }else{
        this.dialogEditFormVisible = false
      }
    },
    // 点击编辑打开编辑弹窗
    async handleOpenEditDialog(row) {
      const memberId = row.id
      const response = await membermodel.findMember(memberId)
      if(response.error_code == 0) {
        this.editform = response.msg
        const value= response.msg.payType
        const obj = payType.find(item=>item.type == value)
        this.editform.payType = obj.name
      }
      this.dialogEditFormVisible = true
    }
  },
  components: {
    // myMain
  }
}
</script>

<style lang="scss" scoped>
.el-forms {
  width: 100%;
  height: 85px;
  align-items: flex-end !important;
  .el-form-item {
    margin-right: 10px !important;
  }
}
.el-select {
  width: 120px !important;
}
.el-table {
  height: 380px !important;
}
</style>
